package whf.framework.util;

import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * @author wanghaifeng
 *
 */
public class SortUtils {
	
	/**
	 * @param objects objects collection
	 * @param property sort property name
	 * @param desc true if sort as desc  else false 
	 * @return 排好序的对象集合
	 */
	public static <T> List<T> sortByProperty(Collection<T> objects,String property,boolean isnumberic, boolean desc){
		List<T> result=Utils.newArrayList();
		try{
			Comparator<? super T> c=new PropertyComparator<T>(property,isnumberic,desc);
			result.addAll(objects);
			Collections.sort(result,c);
		}catch(Exception e){
			result.addAll(objects);
		}
		return result;
	}
	
	public static <T> List<T> sortByProperty(Collection<T> objects,String property){
		return sortByProperty(objects,property,false,false);
	}
	
	public static <T> List<T> sortByProperty(Collection<T> objects, String property, String sort){
		return sortByProperty(objects,property,false,"desc".equalsIgnoreCase(sort));
	}

	static class T implements Comparator {
		public int compare(Object o1, Object o2) {
			T t1 = (T)o1;
			T t2 = (T)o2;
			return t1.name.compareTo(t2.name);
		}
		String name;
		public T(String name) {
			this.name = name;
		}
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
		public String toString() {
			return name;
		}
	}
	
	public static void main(String[] args) {
		List<T> strs = Utils.newArrayList();
		for(int i=0; i < 10; i++){
			strs.add(new T(Math.random() + ""));
		}
		System.out.println(strs);
		System.out.println(sortByProperty(strs, "name"));
	}
}

/**
 * 
 * Comparator对象
 * @author wanghaifeng
 *
 */
class PropertyComparator<T> implements Comparator<T>{
	private boolean desc=false;
	private boolean isNumberic;
	private String propertyName;
	
	public PropertyComparator(String propertyName){
		this.propertyName=propertyName;
	}
	
	public PropertyComparator(String propertyName,boolean isnumberic,boolean desc){
		this.propertyName=propertyName;
		this.desc=desc;
		this.isNumberic = isnumberic;
	}
	
	public int compare(T obj1, T obj2){
		try{
			String value1 = BeanUtils.getStringProperty(obj1,propertyName);
			String value2 = BeanUtils.getStringProperty(obj2,propertyName);
			if(value1==null) value1="";
			if(value2==null) value2="";
			value1=new String(value1.getBytes("GBK"),"ISO-8859-1");
			value2=new String(value2.getBytes("GBK"),"ISO-8859-1");
			if(desc){
				if(this.isNumberic){
					return (int)(Double.parseDouble(value2) - Double.parseDouble(value1));
				}else{
					return value2.compareTo(value1);
				}
			}else{
				if(this.isNumberic){
					return (int)(Double.parseDouble(value1) - Double.parseDouble(value2));
				}else{
					return value1.compareTo(value2);
				}
			}
		}catch(Exception e){
			throw new RuntimeException(e);
		}
	}
}